package cn.service.impl;

import java.sql.Connection;
import java.sql.SQLException;

import cn.dao.BlogListDao;
import cn.dao.JDBCUtils;
import cn.dao.impl.BlogListDaoImpl;
import cn.pojo.BlogList;
import cn.service.BlogListService;

public class BlogListServiceImpl implements BlogListService{
	private BlogListDao blogListDao = new BlogListDaoImpl();
	@Override
	public Boolean addBlog(BlogList blog) {
		Connection connection = null;
		Boolean flag = false;
		try {
			connection = JDBCUtils.getConnection();
			connection.setAutoCommit(false);//关闭事物自动提交
			flag = blogListDao.addBlog(connection, blog);
			connection.commit();
		} catch (ClassNotFoundException | SQLException e) {
			if(connection!=null) {
				try {
					connection.rollback();//事物回滚
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			e.printStackTrace();
		}finally {
			JDBCUtils.close(connection);
		}
		
		return flag;
	}

	@Override
	public Boolean deleteBlogByBlogId(Integer blogId) {
		Connection connection = null;
		Boolean flag = false;
		try {
			connection = JDBCUtils.getConnection();
			connection.setAutoCommit(false);//关闭事物自动提交
			flag = blogListDao.deleteBlogByBlogId(connection, blogId);
			connection.commit();
		} catch (ClassNotFoundException | SQLException e) {
			if(connection!=null) {
				try {
					connection.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			e.printStackTrace();
		}finally{
			JDBCUtils.close(connection);
		}
		return flag;
	}

	@Override
	public Boolean updateBlog(BlogList blog) {
		Connection connection = null;
		Boolean flag = false;
		try {
			connection = JDBCUtils.getConnection();
			connection.setAutoCommit(false);//关闭事物自动提交
			flag = blogListDao.updateBlog(connection, blog);
			connection.commit();
		} catch (ClassNotFoundException | SQLException e) {
			if(connection!=null) {
				try {
					connection.rollback();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			e.printStackTrace();
		}finally{
			JDBCUtils.close(connection);
		}
		return flag;
	}

}
